package com.example.project.projectspringcloud.service.impl.sys;

import com.example.project.projectspringcloud.common.Constants;
import com.example.project.projectspringcloud.common.OperationException;
import com.example.project.projectspringcloud.dao.sys.SysConfigMapper;
import com.example.project.projectspringcloud.entity.Page;
import com.example.project.projectspringcloud.entity.sys.SysConfig;
import com.example.project.projectspringcloud.service.BaseService;
import com.example.project.projectspringcloud.service.sys.SysConfigService;
import com.example.project.projectspringcloud.utils.RedisUtil;
import com.example.project.projectspringcloud.utils.SmsUtil;
import com.example.project.projectspringcloud.utils.StringUtil;
import com.example.project.projectspringcloud.utils.WebUtil;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysConfigServiceImpl extends BaseService<SysConfig, SysConfigMapper> implements SysConfigService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<SysConfig> list(Page page, SysConfig sysConfig) {
        if (page != null) {
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
        }
        return super.mapper.selectAll(sysConfig);
    }

    @Override
    public Map configs(SysConfig sysConfig) {
        List<SysConfig> sysConfigs = super.mapper.selectAll(sysConfig);
        Map result = new HashMap();
        sysConfigs.forEach(t -> {
            result.put(t.getColumn(), t.getValue());
        });
        return result;
    }

    @Override
    public boolean delete(Integer[] idarr) {
        return super.delete(idarr);
    }

    @Override
    public SysConfig get(SysConfig sysConfig) {
        return super.get(sysConfig);
    }

    @Override
    public boolean save(SysConfig sysConfig) {
        return super.save(sysConfig);
    }

    @Override
    public boolean sms(String target, String type) throws OperationException {
        String smsType = "";
        Map sysConfigs = null;
        String templateType = "";

        // 获取开启的短信平台
        SysConfig entity = new SysConfig();
        entity.setType(Constants.SysConfigType.SYS_CONFIG_TYPE_SMS_PLATFORM);
        entity.setUsable(1);
        entity = get(entity);

        // 根据短信平台获取配置
        smsType = entity.getValue();
        entity = new SysConfig(smsType);
        sysConfigs = configs(entity);

        switch (type) {
            case Constants.SMS_VALIDATE_CODE_OF:
                try {
                    // 获取短信模板
                    SysConfig template = new SysConfig();
                    template.setType(Constants.SysConfigType.SYS_CONFIG_TYPE_SMS_TEMPLATE);
                    template.setUsable(1);
                    template = get(template);
                    template = new SysConfig(template.getValue());
                    sysConfigs.putAll(configs(template));
                } catch (Exception e) {
                    logger.error("UserController.mobileModify - GET:", e);
                    throw new OperationException(StringUtil.translateI18N(messageSource, Constants.SystemMessage.SYSTEM_CONFIG_ERROR));
                }
                SmsUtil smsUtil = new SmsUtil();
                smsUtil.setDestNumber(target);
                smsUtil.setSmsType(smsType);
                smsUtil.setSysConfigs(sysConfigs);
                smsUtil.setCount(WebUtil.getRandomNum(4));
                redisUtil.set(Constants.SMS_VALIDATE_CODE_OF + target, smsUtil.getCount(), 300);
                redisUtil.set(Constants.SMS_VALIDATE_CODE_TIME_OF + target, smsUtil.getCount(), 60);
                smsUtil.sendmsgPoint();
                return true;

            default:
                break;
        }
        return false;
    }
}
